<template>
  <!-- 发运方管理 -->
  <div class="shareContainer" ref="shareContainer">
    <!-- 头部搜索功能 -->
    <div class="shareheader">
      <!-- <div class="shareheader_top">
        <div style="min-width:260px;margin: 5px 10px 18px 0px;">
          <span style="font-size: 13px; padding: 0px 8px 0px 0px">发货人</span>
          <el-input v-model="inputname" placeholder="请输入发货人名称" style="width: 210px"></el-input>
        </div>
        <div style="min-width:260px;margin: 5px 10px 18px 0px;">
          <span style="font-size: 13px; padding: 0px 8px 0px 0px">联系方式</span>
          <el-input v-model="inputPhone" placeholder="请输入发货人联系方式" style="width: 210px"></el-input>
        </div>
        <template v-if="!isCustomer">
          <div style="min-width:200px;margin: 5px 10px 18px 0px;">
            <span style="font-size: 13px; padding: 0px 8px 0px 0px">所属组织</span>
            <el-select v-model="siteNumber" filterable placeholder="请选择">
              <el-option v-for="item in originList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div style="min-width:200px;margin: 5px 10px 18px 0px;">
            <span style="font-size: 13px; padding: 0px 8px 0px 0px">所属客户</span>
            <el-select v-model="customerId" remote reserve-keyword :remote-method="PagecustomerPeople" filterable
              placeholder="请选择">
              <el-option v-for="item2 in clientList" :key="item2.id" :label="item2.name" :value="item2.id">
              </el-option>
            </el-select>
          </div>
        </template>
        <div style="min-width:100px;margin: 5px 10px 18px 0px;">
          <el-button type="primary" data-tracker="P22-1" @click="Pagequeryshipping_squery()"
            icon="el-icon-search">查询</el-button>
          <el-button plain @click="seterquery()" data-tracker="P22-2" icon="el-icon-refresh-left">重置</el-button>
        </div>

      </div> -->
    </div>
    <!-- 内容 -->
    <div class="sharecontent">
      <!-- 头部内容 -->
      <div class="sharecontent_header">
        <div class="sharecontent_header_right">
          <el-button v-permission="'company_shipper_create'" type="primary" plain @click="handleClick()"
            data-tracker="P22-3" style="margin:0px 10px 10px 0px">新增</el-button>
          <el-button v-permission="'company_shipper_delete'" type="primary" plain @click="PagedeleteShipping()"
            data-tracker="P22-4" style="margin:0px 10px 10px 0px">删除</el-button>
        </div>
        <div style="display: flex;">
          <div>
            <el-button type="primary" size="mini" plain @click="seterquery"
              style="margin: 0px 10px 0px 0px;">重置</el-button>
            </el-button>
          </div>
          <SettinUp :settype="27" :objSettinUpdata="objSettinUp" key="3"></SettinUp>
        </div>
      </div>
      <!-- 表单内容 -->
      <div class="sharecontent_content">
        <TableHeader ref="TableHeaderList" :header_data_list="header_data_list" :HeaderData="HeaderData"
          :propMap="propMap" :objheader="objheader" @childEvent="handleDelete" @orderIdNumsEvent="handleDeleteType"
          :operation_type="operation_type" @handleClickDetail_second="handleClickDetail_second"
          @handleClickEdit_second="handleClickEdit_second" particulars="finance_invoice_book_list"
          redact="finance_invoice_book_update">
        </TableHeader>
        <!-- <el-table :data="tableData" border stripe style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" fixed width="55"> </el-table-column>
          <el-table-column type="index" fixed label="序号" width="55"></el-table-column>
          <el-table-column fixed prop="name" label="发货人" width="200">
          </el-table-column>
          <el-table-column prop="mobile" label="联系方式" width="200">
          </el-table-column>
          <el-table-column prop="province" label="发货地址" width="280">
          </el-table-column>
          <el-table-column prop="customer_cache_name" label="发货公司" width="200">
          </el-table-column>
          <el-table-column prop="customer_name" label="所属客户" width="200" v-if="!isCustomer">
          </el-table-column>
          <el-table-column prop="branch_name" label="所属组织" width="280" v-if="!isCustomer">
          </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>
          <el-table-column fixed="right" d="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button v-permission="'company_shipper_detail'" data-tracker="P22-5"
                @click="handleClick(scope.row, '1')" type="text" size="small">查看</el-button>
              <el-button v-permission="'company_shipper_update'" data-tracker="P22-6" type="text" size="small"
                @click="handleClick(scope.row, '2')">编辑</el-button>
            </template>
          </el-table-column>
        </el-table> -->
      </div>
        <!-- 分页功能 -->
        <div class="sharecontent_header_left">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryparam.page" :page-sizes="[10, 20, 30, 40]" :page-size="queryparam.page_size"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      <!-- 弹框内容 -->
      <div>
        <el-drawer :direction="direction" :title="title_name" :visible.sync="dialogVisible" :before-close="handleClose"
          size="40%" :wrapperClosable="false">
          <div>
            <el-form :inline="true" class="demo-form-inline" label-width="130px" :model="ruleForm" :rules="rules"
              ref="ruleForm">
              <el-form-item label="发货人" prop="consigner">
                <el-input placeholder="发货人" v-model="ruleForm.consigner" :disabled="disableDetail"></el-input>
              </el-form-item>
              <el-form-item label="联系方式" prop="phone">
                <el-input placeholder="手机号" v-model="ruleForm.phone" :disabled="disableDetail"></el-input>
              </el-form-item>
              <el-form-item label="所属客户" prop="customer_id">
                <el-select v-model="ruleForm.customer_id" remote reserve-keyword :remote-method="searchCustomerList"
                  filterable placeholder="请选择" :disabled="disableDetail" style="width: 165px;">
                  <el-option v-for="item2 in client" :key="item2.id" :label="item2.name" :value="item2.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="发货公司" prop="customer_name">
                <el-input placeholder="发货公司" v-model="ruleForm.customer_name" :disabled="disableDetail"></el-input>
              </el-form-item>
              <div style="width:100%">
                <el-form-item label="发货人地址" prop="setUPvalue">
                  <el-cascader v-model="ruleForm.setUPvalue" placeholder="选择发货地址" :options="options" :props="{
                      value: 'id',
                      label: 'name',
                      children: 'next_level_data',
                    }" :disabled="disableDetail" style="width: 165px;"></el-cascader>
                </el-form-item>
                <el-form-item label="" prop="indetile">
                  <el-input placeholder="发货人详细地址" v-model="ruleForm.indetile" :disabled="disableDetail"></el-input>
                </el-form-item>
              </div>
              <el-form-item label="所属组织" v-if="detailShow">
                <el-select v-model="ruleForm.branch_id" clearable placeholder="请选择" :disabled="disableDetail"
                  style="width: 165px;">
                  <el-option v-for="item in originList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="身份证号">
                <el-input placeholder="身份证号" v-model="ruleForm.id_card" :disabled="disableDetail"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input placeholder="备注" v-model="ruleForm.comment" :disabled="disableDetail"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button :data-tracker="orderstate == '0'?'P22-8':'P22-10'" @click="handleClose()">取 消</el-button>
              <!-- 代表创建的确定 -->
              <el-button type="primary" data-tracker="P22-7" @click="PageAddshipping('ruleForm')"
                v-if="orderstate == '0'">确 定</el-button>
              <!-- 修改的确定 -->
              <el-button type="primary" data-tracker="P22-9" @click="PageAmendshipping('ruleForm')"
                v-if="orderstate == '2'">确 定</el-button>
            </span>
          </div>
        </el-drawer>
      </div>
    </div>
  </div>
</template>

<script>
import TableHeader from "@/page/orderform/components/TableHeader/tableheader.vue";
import SettinUp from "@/page/orderform/components/settingUp/settingUp.vue";
import {
  Addshipping,
  Amendshipping,
  Detailsshipping,
  deleteShipping,
  queryshipping,
  customerPeople,
} from "@/api/information";
import { PagewebsiteList, regionList } from "../common/common";
export default {
  components: { TableHeader, SettinUp },
  data() {
    return {
      title_name: "发货人",
      direction: "rtl",
      inputPhone: "", //手机号查询
      customerId: "",
      // 客户
      client: [],
      clientList: "",
      // 组织下拉框
      originList: [],
      // 分页功能
      total: 0, //总条数
      currentPage4: 1,
      // 地址库
      options: [],
      queryvalue: [], //查询的value
      inputname: "", //发货人名字
      detailvalue: "", //详细地址
      siteNumber: "", //所属组织查询
      deletparam: {
        id: [], //多选删的id
      },
      queryparam: {
        // name: "", //姓名
        // branch_id: 0, //
        // mobile: "",
        page: 1,
        page_size: 20,
        // customer_id: "",
      }, //发货方搜索接口
      ruleForm: {
        consigner: "", //发货人
        setUPvalue: [], //创建vlaue
        indetile: "", //详细地址
        phone: "", //电话
        comment: "", //备注
        branch_id: "", //所属组织
        customer_id: "", //发货公司
        id_card: "", //身份证号
        customer_name: "", //发货公司
      },
      rules: {
        consigner: [
          { required: true, message: "请填写发货人名字", trigger: "blur" },
        ],
        setUPvalue: [
          { required: true, message: "请填写发货地址", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请填写发货人手机号", trigger: "blur" },
        ],
        indetile: [
          { required: true, message: "请填写发货人详细地址", trigger: "blur" },
        ],
        customer_id: [
          { required: true, message: "请填写客户", trigger: "blur" },
        ],
      },
      tableData: [],
      orderstate: "", //新增状态0 编辑状态2
      consignerID: "", //发货人的ID
      dialogVisible: false,
      detailShow: false,
      disableDetail: false,
      isCustomer: false, // 是否是客户
      //
      header_data_list: [],
      propMap: {},
      obj_list: {},
      HeaderData: [],
      waybillIdNums: [],
      totalPage: 0,
      operation_type: 1,
    };
  },
  mounted() {
    // console.log("提交代码");
    this.provincectiy();
    // 发货人列表
    this.Pagequeryshipping();
    this.siteList();
    const user = JSON.parse(localStorage.getItem("user") || "{}");
    this.isCustomer = user.type === 1;
  },
  methods: {
    // 模糊搜索
    searchCustomerList(val, id) {
      var param = {
        name: val + "",
        is_send: "1",
        is_receive: "",
        id: id == undefined ? "" : id + "",
      };
      customerPeople(param).then((res) => {
        var res = res.data;
        this.client = res;
      });
    },
    // 客户管理的人
    PagecustomerPeople(val) {
      var param = {
        name: val + "",
        is_send: "1",
        is_receive: "",
      };
      customerPeople(param).then((res) => {
        var res = res.data;
        this.clientList = res;
      });
    },
    // 组织管理页面
    siteList() {
      PagewebsiteList("3").then((res) => {
        this.originList = res.data;
      });
    },
    provincectiy() {
      regionList(0).then((res) => {
        this.options = res.data;
      });
    },
    // 分页功能
    // 一页有多少条
    handleSizeChange(val) {
      this.queryparam.page_size = val;
      //  数据列表接口
      this.Pagequeryshipping();
    },
    // 当前第几页
    handleCurrentChange(val) {
      this.queryparam.page = val;
      //  数据列表接口
      this.Pagequeryshipping();
    },
    // 处理列表的数据
    DataState(res) {
      var nametable = [];
      for (var i = 0; i < res.length; i++) {
        nametable.push({
          id: res[i].id,
          name: res[i].name,
          mobile: res[i].mobile,
          province:
            res[i].province_name +
            "/" +
            res[i].city_name +
            "/" +
            res[i].district_name +
            "/" +
            res[i].address,
          remark: res[i].remark,
          branch_name: res[i].branch_name,
          customer_name: res[i].customer_name,
          customer_cache_name: res[i].customer_cache_name,
        });
      }
      this.tableData = nametable;
    },
    // 创建发运方
    PageAddshipping(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var paramtime = {
            Name: this.ruleForm.consigner, //姓名
            Mobile: this.ruleForm.phone, //电话
            // branch_id: Number(this.ruleForm.branch_id), //所属组织ID
            Remark: this.ruleForm.comment, //备注
            customer_name: this.ruleForm.customer_name, //收货公司
            province_id: this.ruleForm.setUPvalue[0], //省id
            city_id: this.ruleForm.setUPvalue[1], //市ID
            district_id: this.ruleForm.setUPvalue[2], //区ID
            Address: this.ruleForm.indetile, //详细地址
            customer_id:
              this.ruleForm.customer_id == "" ? 0 : this.ruleForm.customer_id, //发货公司
            id_card: this.ruleForm.id_card, //身份证号
          };
          Addshipping(paramtime).then((res) => {
            if (res.code == "10000") {
              this.$message({
                message: "创建发货人成功",
                type: "success",
              });
              // 关闭页面
              this.closepop();
              // 清空数据
              this.clearList();
              // 刷新列表
              this.Pagequeryshipping();
            }
          });
        }
      });
    },
    // 发运方编辑
    PageAmendshipping(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var paramtime = {
            id: this.consignerID,
            Name: this.ruleForm.consigner,
            Mobile: this.ruleForm.phone,
            // branch_id: Number(this.ruleForm.branch_id),
            Remark: this.ruleForm.comment,
            province_id: this.ruleForm.setUPvalue[0],
            city_id: this.ruleForm.setUPvalue[1],
            district_id: this.ruleForm.setUPvalue[2],
            Address: this.ruleForm.indetile,
            customer_name: this.ruleForm.customer_name, //收货公司
            customer_id:
              this.ruleForm.customer_id == "" ? 0 : this.ruleForm.customer_id,
            id_card: this.ruleForm.id_card, //身份证号
          };
          Amendshipping(paramtime).then((res) => {
            if (res.code == "10000") {
              this.$message({
                message: "修改发货人成功",
                type: "success",
              });
              // 关闭页面
              this.closepop();
              // 刷新列表
              this.Pagequeryshipping();
            }
          });
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    // 发运方详情
    PageDetailsshipping(id) {
      var param = {
        id: id,
      };
      Detailsshipping(param).then((res) => {
        var res = res.data;
        this.ruleForm.consigner = res.name;
        this.ruleForm.phone = res.mobile;
        this.ruleForm.indetile = res.address;
        this.ruleForm.comment = res.remark;
        this.searchCustomerList("", res.customer_id);
        this.ruleForm.customer_id = res.customer_id == 0 ? "" : res.customer_id;
        this.ruleForm.id_card = res.id_card;
        (this.ruleForm.customer_name = res.customer_name), //收货公司
          (this.ruleForm.branch_id =
            res.branch_id == 0 ? "" : Number(res.branch_id));
        this.ruleForm.setUPvalue = [
          res.province_id,
          res.city_id,
          res.district_id,
        ];
      });
    },
    // 发货方删除
    PagedeleteShipping() {
      // var str = JSON.stringify(this.deletparam);
      var param = {
        id: this.waybillIdNums,
      };
      deleteShipping(param).then((res) => {
        if (res.code == 10000) {
          this.$message({
            message: "删除发货人成功",
            type: "success",
          });
          // 刷新列表
          this.Pagequeryshipping();
        }
      });
    },
    // 发货方搜索和列表的回显
    Pagequeryshipping() {
      var param = {
        header_id: 27 + "",
        ...this.queryparam,
        ...this.obj_list,
      };
      queryshipping(param).then((res) => {
        this.orders = res.data.list || [];
        this.orders.unshift({});
        this.header_data_list = this.orders;
        this.HeaderData = res.data.item_list;
        this.Fntransformation(this.HeaderData);
        this.total = res.data.pagination.total;
        // var res = res.data;
        // this.total = res.pagination.total;
        // var data = res.list == null ? [] : res.list;
        // this.DataState(data);
        // this.clearList();
      });
    },
    objheader(obj) {
      var data = {
        header_id: 27 + "",
        ...obj,
        ...this.queryparam,
      };
      this.obj_list = obj;
      queryshipping(data).then((res) => {
        this.header_data_list = res.data.list;
        if (res.code === 10000 && res.data.list !== null) {
          this.header_data_list.unshift({});
          this.total = res.data.pagination.total;
        } else {
          this.header_data_list = [{}];
          this.total = 0;
          this.orderslength = 0;
        }
      });
    },
    Fntransformation(obj) {
      const result = {};
      for (const prop in obj) {
        if (obj.hasOwnProperty(prop)) {
          if (obj[prop].send_name !== "") {
            result[obj[prop].name] = obj[prop].show_name;
          } else {
            result[obj[prop].name] = obj[prop].name;
          }
        }
      }
      this.propMap = result;
    },
    handleDelete(index, id, type) {
      this.handleJumpDetail(id);
    },
    handleDeleteType(value) {
      this.waybillIdNums = value;
    },
    handleClickDetail_second(val) {
      this.handleClick(val, 1);
    },
    handleClickEdit_second(val) {
      this.handleClick(val, 2);
    },
    // 获取id
    handleClick(row, type) {
      if (row == undefined) {
        this.orderstate = "0";
        this.title_name = "创建发货人";
        this.openpop();
        this.detailShow = false;
        this.disableDetail = false;
      } else if (row != "" && type == 1) {
        // 将修改的状态赋值为空
        this.title_name = "查看发货人";
        this.orderstate = "";
        this.PageDetailsshipping(row.id);
        this.openpop();
        this.detailShow = true;
        this.disableDetail = true;
      } else if (row != "" && type == 2) {
        this.title_name = "修改发货人";
        this.orderstate = "2";
        this.consignerID = row.id;
        this.PageDetailsshipping(row.id);
        this.openpop();
        this.detailShow = false;
        this.disableDetail = false;
      }
    },
    // 获取多选删除的id号
    handleSelectionChange(val) {
      var deleid = [];
      for (var i = 0; i < val.length; i++) {
        deleid.push(val[i].id);
      }
      this.deletparam.id = deleid;
    },
    // 关闭弹框的函数
    closepop() {
      this.dialogVisible = false;
    },
    // 打开弹框
    openpop() {
      this.dialogVisible = true;
    },
    // 点击关闭和取消
    handleClose() {
      // 清空添加的数据
      this.clearList();
      // 关闭页面
      this.closepop();
    },
    // 清空回显的数据
    clearList() {
      Object.assign(this.$data.ruleForm, this.$options.data().ruleForm);
    },
    // 重置搜索
    seterquery() {
      // 查询的数据清除
      this.$refs.TableHeaderList.childMethod();
      this.queryparam.page = 1;
    },
    objSettinUp() {
      this.Pagequeryshipping();
    },
    Pagequeryshipping_squery() {
      this.queryparam.page_size = 20;
      this.queryparam.page = 0;
      this.Pagequeryshipping();
    },
  },
};
</script>

<style lang="less" scoped>
.shareContainer {
  padding: 10px 15px;
  background-color: #fff;

  .shareheader {
    margin: 0 auto;

    .shareheader_top {
      display: flex;
      flex-wrap: wrap;
    }
  }
  .sharecontent_header_left {
    display: flex;
    justify-content: flex-end;
    margin: 10px 0px 0px 0px;
  }
  // 内容
  .sharecontent {
    margin: 0 auto;
    background: white;

    // 头部内容
    .sharecontent_header {
      display: flex;
      justify-content: space-between;
      /* .sharecontent_header_right {
          display: inline-block;
        } */
    }

    // 表单内容
    .sharecontent_content {
      // margin-top: 12px;
    }
  }
}

.dialog-footer {
  position: fixed;
  width: 40%;
  text-align: center;
  bottom: 20px;
}
</style>